package com.example.studentmanager.Dao;


import com.example.studentmanager.Pojo.Home;
import com.example.studentmanager.Pojo.Student;
import com.example.studentmanager.Util.Dbtool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class BaseDao {
    private  Dbtool dbtool;
    private  PreparedStatement Pre;
    private  ResultSet  rs;
    private Connection coon;


    //实现层 类
    public ResultSet SelectAll(String sql) throws SQLException {
      coon=  Dbtool.getconnection();
        try {


            Pre=coon.prepareStatement(sql);
            rs=Pre.executeQuery(sql);

            return rs;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //查询 通过 key主键字段  实体类 Student
    public ResultSet SelectByKey(String sql,String key){
        coon=Dbtool.getconnection();
        try {
            Pre=coon.prepareStatement(sql);
            Pre.setObject(1,key);
            rs=Pre.executeQuery();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return rs;
    }


    public  int UpdateStudent(String sql, Student student){
        coon=Dbtool.getconnection();
        int r;

        try {
            System.out.println("看看进入没");
            Pre =  coon.prepareStatement(sql);
            System.out.println("连接成功");
           Pre.setObject(1,student.getName());
            System.out.println("设置第一个参数");
            Pre.setObject(2,student.getCls());
            Pre.setObject(3,student.getGrade());
            Pre.setObject(4,student.getPhone());
            Pre.setObject(5,student.getHome());
            Pre.setObject(6,student.getState());
            Pre.setObject(7,student.getStudentid());
            System.out.println("设置参数完毕");
            r =Pre.executeUpdate();

            //预处理了 不用再次执行把
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return r;
    }
    public int InsertStudent(String sql ,Student student){
        coon=Dbtool.getconnection();
        int r;

        try {
            System.out.println("看看进入没");
            Pre =  coon.prepareStatement(sql);
            System.out.println("连接成功");
            Pre.setObject(2,student.getName());
            System.out.println("新增设置第一个参数");
            Pre.setObject(3,student.getCls());
            Pre.setObject(4,student.getGrade());
            Pre.setObject(5,student.getPhone());
            Pre.setObject(6,student.getHome());
            Pre.setObject(7,student.getState());
            Pre.setObject(1,student.getStudentid());
            System.out.println("设置参数完毕");
            r =Pre.executeUpdate();

            //预处理了 不用再次执行把
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return r;
    }
    public int InsertHome(String sql , Home home){
        coon=Dbtool.getconnection();
        int r;

        try {
            System.out.println("看看进入没");
            Pre =  coon.prepareStatement(sql);
            System.out.println("连接成功");
           Pre.setObject(1,home.getHomeid());
            Pre.setObject(2,home.getPrice());
            Pre.setObject(3,home.getBedid());


            System.out.println("设置参数完毕");
            r =Pre.executeUpdate();

            //预处理了 不用再次执行把
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return r;
    }
}
